package fr.ensisa.sunspot.actor;

import java.io.Serializable;
import java.util.Vector;

import fr.ensisa.sunspot.event.IEvent;
import fr.ensisa.sunspot.event.TemplateEvent;

public class Template implements Serializable {
	private static final long serialVersionUID = 3217264940476702640L;
	
	private Vector<IEvent> events;

	public Template() {
	}

	public Template(int major, int minor, int state) {
		addEvent(new TemplateEvent(major, minor, state));
	}
	
	public Template(int major, int minor, int state, String source) {
		addEvent(new TemplateEvent(major, minor, state, source));
	}
	
	public Template(IEvent event) {
		addEvent(event);
	}

	public Template(Vector<IEvent> events) {
		addEvents(events);
	}

	public void addEvents(Vector<IEvent> events) {
		if (this.events == null)
			events = new Vector<IEvent>(events.capacity());
		events.addAll(events);
	}

	public void addEvent(IEvent event) {
		if (this.events == null)
			events = new Vector<IEvent>(3);
		events.add(event);
	}
	
	public Vector<IEvent> getEvents() {
		if (this.events==null) events = new Vector<IEvent>(3);
		return events;
	}

	public void removeEvent(IEvent event) {
		if (this.events == null)
			return;
		this.events.remove(event);
	}

	public boolean accept(IEvent event) {

		for (IEvent e : this.getEvents()) {
			if (e.getState() != IEvent.STATE_EMPTY) {
				if ((event.getState() & e.getState()) != event.getState())
					continue;
			}
			if (e.getMajor() != IEvent.MAJOR_NO) {
				if ((event.getMajor() & e.getMajor()) != event.getMajor())
					continue;
			}
			if (e.getMinor() != IEvent.MINOR_NO) {
				if ((event.getMinor() & e.getMinor()) != event.getMinor())
					continue;
			}
			if (!e.getSource().equals(IEvent.UNKNOWN_SOURCE)) {
				if (!event.getSource().equals(e.getSource()))
					continue;
			}
			
			return true;
		}
		
		return false;
	}

}
